#!/bin/sh
# Copyright (c) 2011, bkil.hu
# This program is free software and can be distributed under the terms of
# the GNU General Public License v2,
# see COPYING for detailed licensing terms.

# created on 2011-03-04

# TODO: subpage and navigation links

main(){
 local ID=`date "+%Y%m%d"`
 local RAW=raw_html
 local TMP=tmp_html

cat <<EOF |
m1 3
m2 3
duna 3
tv2 3
viasat 3
story 3
rtl 3
hirtv 3
atv 3
viva 3
duna2 3
EOF
 grab $ID $RAW

 # convert $ID $RAW $TMP
}

grab(){
 local ID=$1
 local RAW=$2
 while read CHAN MAXSUB
 do
  echo "$CHAN" >&2
  N=`ls "$RAW/$ID/$CHAN" 2>/dev/null |
     wc -l`
  if [ $N -eq 0 ] ;then
   set_chan $CHAN ||exit 1
   get_pages "$RAW/$ID/$CHAN" $MAXSUB ||exit 1
  fi
 done
}

set_chan() { #TODO
 CHAN=$1
 lookup $CHAN >&2
 tvtime-command SET_STATION `lookup $CHAN`
# mencoder tv:// -ovc copy -nosound -frames 1 -tv freq=`lookup $CHAN` -o /dev/null
}

lookup() {
 cat <<EOF |
m1
m2
rtl
duna
tv2
helyi
story
duna2
hirtv
atv
viasat
12
13
14
15
16
17
18
19
viva
reflektor
cool
EOF
 grep -on -m 1 $1 |
 sed "s/^\([^:]*\):.*$/\1/"
 
# $HOME/bin/getfreq.sh $1
}

get_pages() {
 DEST=$1
 MAXSUB=$2

 mkdir -p "$DEST" ||exit 1
 TO=`expr 24 \* $MAXSUB + 5`
 for a in `seq 100 899`
 do
  printf "%03i\n" $a
  for b in `seq 0 21`
  do
   printf "%03i.%02i\n" $a $b
  done
 done |
 xargs alevt-cap -format html \
  -charset latin-2 \
  -name $DEST/`page_to_filename %p` -timeout $TO
 true
 # reveal
}


convert() {
 local ID=$1
 local RAW=$2
 mkdir -p "$RAW/$ID" ||exit 1
 cd "$RAW/$ID" ||exit 1

 for a in * ;do
  cd $a ||exit 1

  fix_charset
  create_links
  fix_images
  add_navbar
  symlink_onlysubs

  cd ..||exit 1
 done

 cd ../.. ||exit 1
}

symlink_onlysubs() {
 #TODO: work around symlinks
 for S in `page_to_filename \* \*`
 do
  P=`echo "$S" |
     sed "s/^ttext-\([^.]*\).*$/ttext-\1.html/"`
  I=`get_subpage_index $S`
  if [ -L $P ] ; then
   OLD=$(get_subpage_index $(readlink $P))
   if [ $I -lt $OLD ] ;then
    ln -sf $S $P
   fi
  elif [ ! -f $P ] ;then
   ln -s $S $P
  fi
 done
}

get_subpage_index() {
 echo "$1" |
 sed "s/^ttext-[^.]*\.\([^.]*\)\.html$/\1/"
}

fix_charset() { : #TODO
 return
 # case $CHAN in ...
 #atv
 sed -i "
  s/Š/É/g
  s/Ž/Á/g
  s/š/é/g
  " *.html
 #hirtv
 sed -i "
  s/©/É/g
  s/¹/é/g
  " *.html
# m1
#  s/Û/Ű/g
#  s/Õ/Ő/g

# áíűőüöúóé ÁÍŰŐÜÖÚÓÉ
}

page_to_filename() {
 PAGE=$1
 SUB=$2
 E="html"
 if [ $# = 2 ] ;then
  echo "ttext-$PAGE.$SUB.$E"
 else
  echo "ttext-$PAGE.$E"
 fi
}

create_links() {
 T1=`tempfile -p "tele-" -s ".teletext.tmp"`
 for a in *.html
 do
  if [ ! -L $a ] ;then
   # why does sed generate an error with the '-i' option??
   sed -r "s~([^0-9])([1-8][0-9][0-9])([^0-9<]|<[^/]|</[^aA]|</a[^ ])~\1<a href=\"ttext-\2.html\">\2</a>\3~g" $a > $T1
   mv $T1 $a
  fi
 done
 rm $T1 2>/dev/null
}

check_exist() { #TODO - unused
 T1=`tempfile -p "tele-" -s ".teletext.tmp"`
 grep -oE "[^0-9]([1-8][0-9][0-9])[^0-9]" *.html |
 sed -r "s~[^0-9]([1-8][0-9][0-9])[^0-9]~\1~" |
 sort |
 uniq |
 while read P
 do
  if [ -f `page_to_filename $P` ] ;then
   echo "$P"
  fi
 done
 rm $T
}

fix_images() { : #TODO
 local IMGCHAR="#"
# sed -i "s/$IMGCHAR/<img src=\"\" alt=\"$IMGCHAR\">/g" *.html
}

add_navbar() { : #TODO
}

main "$@"
